package com.example.controller;

import com.example.dto.UserLoginDTO;
import com.example.dto.UserRegisterDTO;
import com.example.service.UserService;
import com.example.util.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * ClassName:WebController
 * Package:com.example.controller
 * User:HP
 * Date:2025/8/23
 * Time:下午4:06
 * Author 周东汉
 * Version 1.0
 * Description:
 */

@Slf4j
@RestController
@RequestMapping("/api/public")
public class WebController {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private UserService userService;

    @GetMapping("/hello")
    public String hello() {
//        String key = "book_borrow:" + 1 + ":" + 1;
        StringBuilder sb = new StringBuilder();
        sb.append("borrowBook:").append(1).append(":").append(2);
        String key = sb.toString();
        redisTemplate.opsForValue().set(key, "borrowed", 10, TimeUnit.SECONDS);
        return "Hello World";
    }

    @GetMapping("/test")
    public String test() {
        StringBuilder sb = new StringBuilder();
        sb.append("borrowBook:").append(1).append(":").append(2);
        String key = sb.toString();
        redisTemplate.delete(key);
        return "Hello World";
    }

    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        return userService.login(userLoginDTO);
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        return userService.register(userRegisterDTO);
    }

    @PostMapping("logout")
    public Result logout() {
        return userService.logout();
    }

}
